﻿@model GolfLeague.EditModels.ScoringCalculationEditModel

<script type="text/javascript">
    $(document).ready(function () {
        $('#dialog').dialog({
            autoOpen: false,
            model: true
        });
        $('#formulaDialog').dialog({
            autoOpen: false,
            model: true
        });
        $('button').button();
        $('input:button').button();
        $('input:submit').button();
        $('input:reset').button();
        $('div#buttonlist a').hover(
		    function () { $(this).addClass('ui-state-hover'); },
		    function () { $(this).removeClass('ui-state-hover'); }
	    );
    });

    $(function () {
        $('.VariableButton').click(function (e) {
            e.preventDefault();
            var currentFormula = document.getElementById('NewScoringCalculation_Formula');
            currentFormula.value = currentFormula.value.substring(0, currentFormula.selectionStart) + $(this).text() + currentFormula.value.substring(currentFormula.selectionStart);
        });
    });

    $(function () {
        $('.ClearButton').click(function (e) {
            e.preventDefault();
            $("#formulaDialog").dialog("option", "buttons", {
                "Yes": function () {
                    $(this).dialog("close");
                    document.getElementById('NewScoringCalculation_Formula').value = '';
                },
                "No": function () {
                    $(this).dialog("close");
                    return false;
                }
            });
            $("#formulaDialog").dialog("open");
        });
    });

    $(function () {
        $(".DeleteButton").click(function (e) {
            e.preventDefault();
            var recordToDelete = $(this).attr("id");
            recordToDelete = recordToDelete.replace("delete-", "");

            $("#dialog").dialog("option", "buttons", {
                "Yes": function () {
                    $(this).dialog("close");
                    if (recordToDelete != '') {
                        $.post("/ScoringMethod/ScoringCalculationsDelete", { "id": recordToDelete },
                        function (data) {
                            $('#row-' + recordToDelete).fadeOut('slow');
                        });
                    }
                },
                "No": function () {
                    $(this).dialog("close");
                    return false;
                }
            });
            $("#dialog").dialog("open");
        });
    });

    $(function () {
        $(".EditButton").click(function (e) {
            e.preventDefault();
            var recordToEdit = $(this).attr("id");
            recordToEdit = recordToEdit.replace("edit-", "");

            if (recordToEdit != '') {
                window.location = '/ScoringMethod/ScoringCalculationsEdit/' + recordToEdit;
            }
        });
    });

    $(function () {
        $(".CancelEditButton").click(function (e) {
            e.preventDefault();
            window.location = '/ScoringMethod/ScoringCalculations';
        });
    });

</script>

@{
    ViewBag.Title = "ScoringCalculations";
}

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Add Scoring Calculation</legend>

        @Html.HiddenFor(model => model.NewScoringCalculation.ScoringCalulationID)

        <div class="editor-label">
            @Html.LabelFor(model => model.NewScoringCalculation.CalculationName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NewScoringCalculation.CalculationName)
            @Html.ValidationMessageFor(model => model.NewScoringCalculation.CalculationName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.NewScoringCalculation.Formula)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.NewScoringCalculation.Formula, new { style = "vertical-align:middle;width:400px" })
            <a href="#" title="Clear Forumula" style="vertical-align:middle" class="ClearButton ui-state-default ui-corner-all ui-icon ui-icon-cancel"></a>
            @Html.ValidationMessageFor(model => model.NewScoringCalculation.Formula)
        </div>

        <fieldset>
            <legend>Operators</legend>
            <br />
            <div id="operatorButtonlist">
                <button class="VariableButton" name="(" title="Left Parentheses">(</button>
                <button class="VariableButton" name=")" title="Right Parentheses">)</button>
                <button class="VariableButton" name="+" title="Plus">+</button>
                <button class="VariableButton" name="-" title="Minus">-</button>
                <button class="VariableButton" name="*" title="Multiply">*</button>
                <button class="VariableButton" name="/" title="Divide">/</button>
                <button class="VariableButton" name=">" title="Greater Than">></button>
                <button class="VariableButton" name="<" title="Less Than"><</button>
                <button class="VariableButton" name="==" title="Equals">==</button>
                <button class="VariableButton" name="!=" title="Not Equals">!=</button>
                <button class="VariableButton" name="&&" title="AND">&&</button>
                <button class="VariableButton" name="||" title="OR">||</button>
            </div>
        </fieldset>

        <fieldset>
            <legend>@Html.ActionLink("Variables", "ScoringVariables")</legend>
            <br />
            <div id="variableButtonlist">
                @foreach (var item in Model.ScoringVariables)
                {
                    <button class="VariableButton" id="variable-@item.ScoringVariableID" name="@item.DisplayName" title="@item.Description">@item.DisplayName</button>
                }
            </div>
        </fieldset>

        <p>
        @if (Model.NewScoringCalculation.ScoringCalulationID < 1)
        {
            <input type="submit" value="Add" />
        }
        else
        {
            <input type="submit" value="Save" />
            <input type="reset" class="CancelEditButton" value="Cancel" />
        }
        </p>
        <div id="dialog" title="Confirm Delete">Are you sure you want to delete this calculation?</div>
        <div id="formulaDialog" title="Confirm Delete">Are you sure you want to clear the current forumla?</div>
    </fieldset>
}

<fieldset>
    <legend>Scoring Calculations</legend>
    <table>
        <tr>
            <th>
                Calculation Name
            </th>
            <th>
                Formula
            </th>
            <th></th>
        </tr>

@foreach (var item in Model.ScoringCalculations) {
        <tr id="row-@item.ScoringCalulationID">
            <td>
                @item.CalculationName
            </td>
            <td>
                @item.Formula
            </td>
            <td>
                <div id="buttonlist">
                    <a href="#" title="Edit Calculation" class="EditButton ui-state-default ui-corner-all ui-icon ui-icon-pencil" id="edit-@item.ScoringCalulationID"></a>
                    <a href="#" title="Delete Calculation" class="DeleteButton ui-state-default ui-corner-all ui-icon ui-icon-trash" id="delete-@item.ScoringCalulationID"></a>
                </div>
            </td>
        </tr>
}

    </table>
</fieldset>